package com.example.petback.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.petback.entity.MsgEntity;
import com.example.petback.model.OrderModel;
import com.example.petback.service.ItemService;
import com.example.petback.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/petback/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private ItemService itemService;

    @RequestMapping("/getOrdersByStatus/{status}")
    public MsgEntity getOrdersByStatus(@PathVariable Integer status){
        List<OrderModel> orders = orderService.getOrdersByStatus(status);
        return new MsgEntity("SUCCESS","200",orders);
    }
    @RequestMapping("/getOrdersByItemid")
    public MsgEntity getOrdersByItemid(){
        String likeitemid="EST-";
        List<OrderModel> orders2 = orderService.getOrdersByItemid(likeitemid);
        return new MsgEntity("SUCCESS","200",orders2);
    }
    @RequestMapping("/getOrderByNumber/{number}")
    public MsgEntity getOrderByNumber(@PathVariable String number){
        OrderModel order = orderService.getOrderByNumber(number);
        return new MsgEntity("SUCCESS","200",order);
    }

    @PostMapping("/updateOrderByNumber")
    public MsgEntity updateOrderByNumber(@RequestBody JSONObject jsonObject){
        String number = jsonObject.getString("number");
        String address = jsonObject.getString("address");
        String name = jsonObject.getString("name");
        String phone = jsonObject.getString("phone");
        orderService.updateOrderByNumber(address,name,phone,number);
        return new MsgEntity("SUCCESS","200",true);
    }

    @PostMapping("/updateCompanyByNumber")
    public MsgEntity updateCompanyByNumber(@RequestBody JSONObject jsonObject){
        String number = jsonObject.getString("number");
        String company = jsonObject.getString("company");
        orderService.updateCompanyByNumber(number,company);
        return new MsgEntity("SUCCESS","200",true);
    }

    @PostMapping("/updateStatusByNumber")
    public MsgEntity updateStatusByNumber(@RequestBody JSONObject jsonObject){
        String number = jsonObject.getString("number");
        orderService.updateStatus(number);
        return new MsgEntity("SUCCESS","200",true);
    }

    @GetMapping("/getPictureByNumber/{number}")
    public MsgEntity getPictureByNumber(@PathVariable String number){
        OrderModel order = orderService.getOrderByNumber(number);
        String picture = itemService.getPicture(order.getItemid());
        return new MsgEntity("SUCCESS","200",picture);
    }

}
